package com.example.ecas.controller;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.CaseMapper;
import com.example.ecas.persistence.PatientMapper;
import com.example.ecas.pojo.Case;
import com.example.ecas.pojo.Patient;
import com.example.ecas.service.AbstractsService;
import com.example.ecas.pojo.Tag;
import com.example.ecas.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

// 患者画像
@RestController
public class PatientController {

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    CaseMapper caseMapper;

    @Autowired
    PatientService patientService;

    @Autowired
    AbstractsService abstractsService;

    // 获取患者画像,返回的数据的列表list list[0]为patient对象,list[1]为case列表
    @GetMapping(value = "/patient")
    @ResponseBody
    public CommonResponse<Object> sendSmsForLogin(@RequestParam("patientId") String id, HttpSession session) {
        int patId;
        try {
            patId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的患者ID类型");
        }
        Patient patient = patientMapper.getPatient(patId);
        if (patient == null) {
            return CommonResponse.creatForError("查询的患者不存在");
        }
        List<Case> cases = caseMapper.getPatientCases(patId);
        List<Object> list = new ArrayList<>();
        list.add(patient);
        list.add(cases);
        return CommonResponse.creatForSuccess("获取成功", list);
    }

    // 获取患者画像,返回的数据的列表list
    // list[0]为patient对象, list[1]为tags
    // list[2]为currentCase, list[3]为tinyCase列表
    // list[4]为摘要, list[5]为base64患者词云
    @GetMapping(value = "patientImg")
    @ResponseBody
    public CommonResponse<Object> patientImg(@RequestParam("patientId") String id, HttpSession session) {
        int patId;
        try {
            patId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的患者ID类型");
        }
        List<Object> list = patientService.getPatImage(patId);
        return CommonResponse.creatForSuccess(list);
    }

    @GetMapping(value = "/patient/tags")
    @ResponseBody
    public CommonResponse<List<Tag>> patientTags(@RequestParam("patientId") String id, HttpSession session) {
        int patId;
        try {
            patId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的患者ID类型");
        }
        List<Tag> tagList = patientService.getPatTags(patId);
        if(tagList == null) {
            return CommonResponse.creatForError("患者没病！！！");
        }
        return CommonResponse.creatForSuccess(tagList);
    }
}
